const dayjs = require("dayjs");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    allNum: '',
    corpCredit: '',
    selectRisk: '信用风险',
    riskList: [{
        type: 1,
        id: 'credit_risk',
        name: '信用风险',
        name1: '信用',
        name2: '风险'
      },
      {
        type: 2,
        id: 'opr_risk',
        name: '经营风险',
        name1: '经营',
        name2: '风险'
      },
      {
        type: 3,
        id: 'rel_risk',
        name: '关联风险',
        name1: '关联',
        name2: '风险'
      },
      // {
      //   type: 4,
      //   name: '市场风险',
      //   name1: '市场',
      //   name2: '风险'
      // },
    ],
    selectPickerList1: '全部',
    selectPickerList2: '全部',
    selectId1: '',
    selectId2: '',
    pickerList1: [{
        id: 'all',
        name: '全部'
      },
      {
        id: 'rel_risk',
        name: '关联风险'
      },
      {
        id: 'opr_risk',
        name: '经营风险'
      },
      {
        id: 'credit_risk',
        name: '信用风险'
      },
      // {
      //   id: '',
      //   name: '橙色预警'
      // },
    ],
    pickerList2: [{
        id: 0,
        name: '全部'
      },
      {
        id: 7,
        name: '近7天'
      },
      {
        id: 30,
        name: '近30天'
      },
      {
        id: 182,
        name: '近半年'
      },
      {
        id: 365,
        name: '近一年'
      }
    ],
    tabList: [{
      url: 'https://zx-1311146360.cos.ap-shanghai.myqcloud.com/attention/tab_list.png',
      title: '风险列表'
    }, {
      url: 'https://zx-1311146360.cos.ap-shanghai.myqcloud.com/attention/icon_dongtai.png',
      title: '风险动态'
    }],
    selectName: '风险列表',
    fxList: [{
        time: '2022-02-11 09:34:09',
        level: '1',
        text: '企业营收竞争力较弱',
        list: [{
          level: '1',
          text: '企业营收竞争力较弱',
          name: '信用风险',
        }]
      },
      {
        time: '2022-02-11 07:34:09',
        level: '2',
        text: '企业营收竞争力较弱',
        list: [{
            level: '1',
            text: '企业营收竞争力较弱',
            name: '经营风险',
          },
          {
            level: '2',
            text: '企业营收竞争力较弱',
            name: '关联风险',
          },
          {
            level: '3',
            text: '企业营收竞争力较弱',
            name: '市场风险',
          }
        ]
      },
    ],
    showCanvas: false,
    list: [],
    name1: '',
    name2: '',
    lineType: 'credit_risk'
  },
  selectRisk(e) {
    this.setData({
      selectRisk: e.currentTarget.dataset.name,
      lineType: e.currentTarget.dataset.id,
    })
    this.getLineInfos()
  },
  detailShow(e) {
    const params = {
      corpCredit: this.data.corpCredit,
      riskCode: e.currentTarget.dataset.riskcode,
      recentDay: this.data.selectId2 == 0 ? '' : this.data.selectId2
    };
    getApp().globalData.request(
      "/user/query_risk_content", params,
      "POST"
    ).then(res => {
      console.log(res, 'res')
      let data = ''
      let detailShow = e.currentTarget.dataset.detailshow
      let iname = e.currentTarget.dataset.iname
      let list = this.data.list
      for (let i in list) {
        for (let a in list[i]) {
          if (list[i][a].iname == iname) {
            list[i][a].detailShow = !list[i][a].detailShow
            list[i][a].listTwo = res.data
          }
        }
      }
      this.setData({
        list: list
      })
    })
  },
  selectOne(e) {
    this.setData({
      selectName: e.currentTarget.dataset.name
    })
  },
  onChange1(e) {
    const id = +e.detail.value
    const obj = this.data.pickerList1[id]
    this.setData({
      selectPickerList1: obj.name,
      selectId1: obj.id
    })
    // 重新查询
    this.getDetails()
  },
  onChange2(e) {
    const id = +e.detail.value
    const obj = this.data.pickerList2[id]
    this.setData({
      selectPickerList2: obj.name,
      selectId2: obj.id
    })
    // 重新查询
    this.getDetails()
    this.getLineInfos()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options, 'options')
    // for (let i in this.data.pickerList1) {
    //   if (this.data.pickerList1[i].name == options.selectList1) {
    //     console.log('成功')
    //     this.setData({
    //       selectId1: this.data.pickerList1[i].id
    //     })
    //   }
    // }
    // for (let i in this.data.pickerList2) {
    //   if (this.data.pickerList2[i].name == options.selectList2) {
    //     this.setData({
    //       selectId2: this.data.pickerList2[i].id
    //     })
    //   }
    // }
    const corpCredit = options.corpCredit
    this.setData({
      corpCredit: corpCredit,
      name1: options.name1 || '',
      name2: options.name2 || '',
    })
    console.log(this.data.selectId1, this.data.selectId1, '222')
    // const testInfos = {
    //   title: '风险动态趋势-信用风险',
    //   x: [202202, 202203, 202204, 202205, 202206, 202207, 202208],
    //   y: [500, 300, 800, 600, 200, 100, 40]
    // }
    this.getDetails()
    this.getLineInfos()
  },

  async getDetails() {
    this.setData({
      allNum: '',
      list: []
    })
    const params = {
      corpCredit: this.data.corpCredit,
      riskType: this.data.selectId1 == 'all' ? '' : this.data.selectId1,
      recentDay: this.data.selectId2 == 0 ? '' : this.data.selectId2
    };
    console.log(params, 'params1122221')
    this.setData({
      loading: true,
    });
    wx.showLoading({
      title: "加载中",
    });
    const res = await getApp().globalData.request(
      // "/user/query_risk_detail",
      "/user/query_risk_detailV2", {
        ...params
      },
      "POST"
    );
    this.setData({
      loading: false,
    });
    setTimeout(function () {
      wx.hideLoading();
    }, 800);
    // 原始列表数据
    if (res.data.length > 0) {
      let showList = this.findSameList(res.data)
      for (let i in showList) {
        for (let a in showList[i]) {
          showList[i][a].detailShow = false
        }
      }
      this.setData({
        allNum: res.data.length,
        list: showList
      });

    } else {
      this.setData({
        allNum: 0,
      });
    }
  },

  async getLineInfos() {
    this.setData({
      showCanvas: false,
    })

    const params = {
      corpCredit: this.data.corpCredit,
      riskType: this.data.lineType,
      recentDay:this.data.selectId2 == 0 ? '' : this.data.selectId2
    }
    console.log(params, 'paramsparams11')
    const res = await getApp().globalData.request(
      "/user/query_risk_linechartV2", {
        ...params
      },
      "POST"
    );

    // 处理横坐标的日期
    const dataX = res.data.x
    // const dataX = res.data.x.map(val => dayjs(val).format('MM月DD日'))
    const obj = {
      years: [1],
      xData: dataX,
      yData: [res.data.y],
      title: res.data.title,
      type: "line",
    }
    console.log(obj, 'obj')
    this.setData({
      showCanvas: true,
      echartsObj: obj
    })
  },

  // 查找重复的数据,并且重新排列
  findSameList(item) {
    const needList = JSON.parse(JSON.stringify(item));
    needList.forEach(pre => {
      pre.compareTime = dayjs(pre.createTime).unix()
    })
    // 先去重复
    const singleList = this.removeDuplicateObj(needList).sort((a, b) => b.compareTime - a.compareTime);
    const showList = [];
    singleList.forEach((val, index) => {
      const itemList = [];
      needList.forEach((pre) => {
        if (val.compareTime === pre.compareTime) {
          itemList.push(pre);
        }
      });
      showList.push(itemList);
    });
    return showList;
  },

  removeDuplicateObj(arr) {
    let obj1 = {};
    let arr1 = [];
    arr1 = arr.reduce((newArr, next) => {
      obj1[next.compareTime] ? "" : (obj1[next.compareTime] = 1 && newArr.push(next));
      return newArr;
    }, []);
    return arr1;
  },

  viewDetail() {
    wx.navigateTo({
      url: `/pages/mapDetail/mapDetail?corpKey=${this.data.corpCredit}`
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {},
});